
#include "dev_flash.h"
#include <string.h>


static uint8_t flash_data[4096*2] = {0};

static struct flash_pages_layout dev_layout[] = {
        /* Flash Layout */
	{.pages_count = 2, .pages_size = KB(4)},
	//{.pages_count = 31, .pages_size = KB(4)},
};


static const struct flash_parameters flash_device_parameters = {
	.write_block_size = 4,
	.erase_value = 0,
};


static void flash_pages_layout(const struct flash_pages_layout **layout,
			 size_t *layout_size)
{
	*layout = dev_layout;
	*layout_size = ARRAY_SIZE(dev_layout);
}


static const struct flash_parameters *flash_device_get_parameters(void)
{
	return &flash_device_parameters;
}


static int flash_device_read(off_t offset,
			    void *data,
			    size_t len)
{	
	uint8_t *buf = (uint8_t *)data;

        memcpy(buf, &flash_data[offset], len);
	return 0;
}


static int flash_device_erase(off_t offset,
			     size_t len)
{
	int rc = 0;

        memset(&flash_data[offset] , 0xff , len);
	return rc;
}

static int flash_device_write(off_t offset,
			     const void *data, size_t len)
{
	int rc = 0;
      	uint8_t *buf = (uint8_t *)data;

        memcpy(&flash_data[offset], buf, len);
	return rc;
}


static const struct flash_device_t flash_device_api = {
	.erase = flash_device_erase,
	.write = flash_device_write,
	.read = flash_device_read,
	.get_parameters = flash_device_get_parameters,
	.page_layout = flash_pages_layout,
};


const void* flash_device_get_api(void)
{
    return &flash_device_api;
} 